package com.live.dengdengtv.activity.live;

import android.app.Activity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.live.dengdengtv.R;
import com.live.dengdengtv.bean.gson.LiveInfoItem;

import java.util.ArrayList;


/**
 * 直播列表的Adapter
 */
public class LiveShowAdapter extends ArrayAdapter<LiveInfoItem> {
    private static String TAG = "LiveShowAdapter";
    private int resourceId;
    private Activity mActivity;
    private class ViewHolder{
        TextView tvTitle;
        TextView tvHost;
        TextView tvMembers;
//        TextView tvAdmires; //点赞数
//        TextView tvLbs;       //位置
        ImageView ivCover;
        ImageView ivAvatar;
    }

    public LiveShowAdapter(Activity activity, int resource, ArrayList<LiveInfoItem> objects) {
        super(activity, resource, objects);
        resourceId = resource;
        mActivity = activity;

    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView != null) {
            holder = (ViewHolder)convertView.getTag();
        } else {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.live_single, null);

            holder = new ViewHolder();
            holder.ivCover = (ImageView) convertView.findViewById(R.id.itermbg_iv);
            holder.tvTitle = (TextView) convertView.findViewById(R.id.liveTopic_tv);
            holder.tvHost = (TextView) convertView.findViewById(R.id.host_name);
            holder.tvMembers = (TextView) convertView.findViewById(R.id.live_members_tv);
//            holder.tvAdmires = (TextView) convertView.findViewById(R.id.praises);
//            holder.tvLbs = (TextView) convertView.findViewById(R.id.live_lbs);
            holder.ivAvatar = (ImageView) convertView.findViewById(R.id.avatar_iv);

            convertView.setTag(holder);
        }

        LiveInfoItem data = getItem(position);
        if (!TextUtils.isEmpty(data.getCover())){
            RequestManager req = Glide.with(mActivity);
            req.load(data.getCover()).into(holder.ivCover);
        }else{
            holder.ivCover.setImageResource(R.drawable.cover_background);
        }
/*
        if (null == data.getLive_room_id() || TextUtils.isEmpty(data.getCover())){
            // 显示默认图片
            holder.ivCover.setImageResource(R.mipmap.head_default);
        }else{
            RequestManager req = Glide.with(mActivity);
            req.load(data.getCover()).transform(new GlideCircleTransform(mActivity)).into(holder.ivAvatar);
        }*/

        holder.tvTitle.setText("#"+data.getTitle()+"#");
        if (!TextUtils.isEmpty(data.getCreator_nickname())){
            holder.tvHost.setText("" + data.getCreator_nickname());
        }else{
            holder.tvHost.setText(data.getCreator_id());
        }
        holder.tvMembers.setText(""+data.getCount_of_looker() +" 在看");
//        holder.tvAdmires.setText(""+data.getAdmireCount());
//        if (!TextUtils.isEmpty(data.getLbs().getAddress())) {
//            holder.tvLbs.setText(UIUtils.getLimitString(data.getLbs().getAddress(), 9));
//        }else{
//            holder.tvLbs.setText(getContext().getString(R.string.live_unknown));
//        }

        return convertView;
    }
}
